#!/bin/bash
# date: 2022/11/24
# auth: frealess11
# desc: install grafana

ip_local=$(/usr/sbin/ip add | grep inet | grep global | grep -Ev 'docker' | awk '{print $2}' | awk -F/ '{print $1}' | tail -1)
port=3000
addr=${ip_local}:${port}
docker_name="grafana_${port}"
image="grafana/grafana:9.1.5"
conf=$(pwd)/conf

docker_start() {
  docker run -d \
    --name ${docker_name} \
    -m 1024m \
    -u root \
    -p ${port}:3000 \
    --restart=always \
    --log-opt max-size=100m \
    --log-opt max-file=10 \
    -v /etc/localtime:/etc/localtime:ro \
    -v ${conf}:/etc/grafana \
    ${image} \
    -config=/etc/grafana/grafana.ini

}

docker_stop() {
  docker stop ${docker_name}
  docker rm ${docker_name}
}

docker_exec() {
  docker exec -it ${docker_name} sh
}

docker_logs() {
  docker logs --tail=10 ${docker_name}
}

case $1 in
start)
  docker_start
  ;;
stop)
  docker_stop
  ;;
exec)
  docker_exec
  ;;
logs)
  docker_logs
  ;;
*)
  echo "$0 start|stop|exec|logs"
  ;;
esac
